<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('jumpurl.quick Search Fields') })"
        >
            <el-button type="primary" style="margin-left: 10px" @click="handleImport('importcadre')">{{ '导入信息' }}</el-button>
            <el-tooltip :content="t('导出信息')"  style="margin-left: 10px" placement="top">
                <el-button  type="success"  :disabled="baTable.table.selection!.length > 0 ? false : true" @click="expottTemplateClick('exmportcadre')">
                    {{ '导出信息' }}
                </el-button>
            </el-tooltip>
        </TableHeader>
        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #linkurl>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column prop="status" label="状态" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span style="display: inline-block; width: 10px; height: 10px; border-radius: 50%"
                                  :class="'status-' + scope.row.status"></span>
                            <span style="margin-left: 10px">{{ scope.row.status_text }}</span>
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>
        <!-- 表单 -->
        <PopupForm />
        <Importcadre />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import Importcadre from './Importcadre.vue'
defineOptions({
    name: 'jumpurl',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/Jumpurl/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('jumpurl.id'), prop: 'id', align: 'center', width: 70, operator: false, sortable: 'custom' },
            { label: t('jumpurl.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: false, sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('jumpurl.status'), prop: 'status', align: 'center', render: 'switch', operator: 'eq', sortable: false, replaceValue: { '0': t('jumpurl.status 0'), '1': t('jumpurl.status 1') } },
            { label: t('jumpurl.type'), prop: 'type', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { opt0: t('jumpurl.type opt0'), opt1: t('jumpurl.type opt1') } },
            { label: t('jumpurl.expired_time'), prop: 'expired_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('jumpurl.name'), prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('jumpurl.urlkey'), prop: 'urlkey', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('跳转地址'), prop: 'linkurl',render:'url',target:"_blank", align: 'center', operatorPlaceholder: t('Fuzzy query')},
            { label: t('jumpurl.urlgroup__name'), prop: 'urlgroup.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, 'status'],
    },
    {
        defaultItems: { status: '1', type: 'opt0' },
    }
)

const api = baTable.api as baTableApi
provide('baTable', baTable)

const handleImport = (action: string) => {
    baTable.form.operate! = action
    baTable.form.loading = false
    baTable.table.extend!.showPreImport = true
    baTable.form.extend!.fileUploadStatus = 'wait'
}

// 下载模板按钮
const expottTemplateClick = (type: string) => {
    exportcadreHandle('')
}

const exportcadreHandle = (typeval: string) => {
    const operate = 'exportcadre'
    api.postData(operate, { ids: baTable.getSelectionIds(), template: typeval })
        .then((res:any) => {
            window.location.href = res.data.document_url
        })
        .finally((err:any) => {})
}
onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.showComSearch = true
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
